package com.ext.portlet.community.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.ext.portlet.community.model.CommunityExtra;
import com.ext.portlet.community.service.base.CommunityExtraLocalServiceBaseImpl;
import com.liferay.portal.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.model.Group;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.util.comparator.GroupNameComparator;


public class CommunityExtraLocalServiceImpl
    extends CommunityExtraLocalServiceBaseImpl {
	
	
	public CommunityExtra getCommunityByGroupId(long groupId) throws SystemException {
		
		return communityExtraPersistence.fetchByGroupId(groupId);
		
	}
	
	public List<Group> search(
			long companyId, String name, String description,
			LinkedHashMap<String, Object> params, int start, int end,
			OrderByComparator obc, int type_, int category)
		throws SystemException {
		
		if (obc == null) {
			obc = new GroupNameComparator(true);
		}
		
		List<Group> groupList =  GroupLocalServiceUtil.search(companyId, name, description, params, start, end, obc);
		
		List<Long> groupIds = new ArrayList<Long>();
		List<Group> results = new ArrayList<Group>();
		
		if(type_ > 0){
			
			// default communities
			
			List<CommunityExtra> commExtraList = getByType(type_, category);			
			
			
			for(CommunityExtra extra : commExtraList){
				groupIds.add(extra.getGroupId());
				
			}
			
			for(int i=0; i<groupIds.size(); i++){
				for(Group group : groupList){
					if(group.getGroupId() == groupIds.get(i)){
						results.add(group);
					}					
				}
			}
			
			
			if(type_ == 1){
				
				groupIds = new ArrayList<Long>();
				List<CommunityExtra> commSchool = getByType(5, category);
				for(CommunityExtra extra : commSchool){
					groupIds.add(extra.getGroupId());
					
				}
				for(int i=0; i<groupIds.size(); i++){
					for(Group group : groupList){
						if(group.getGroupId() == groupIds.get(i)){
							results.add(group);
						}					
					}
				}
				if(category == 3){  // Global Community 
					List<CommunityExtra> commGuest = getByType(4, 1);
					groupIds = new ArrayList<Long>();
					
					for(CommunityExtra extra : commGuest){
						groupIds.add(extra.getGroupId());
						
					}
					
					for(int i=0; i<groupIds.size(); i++){
						for(Group group : groupList){
							if(group.getGroupId() == groupIds.get(i)){
								results.add(group);
							}					
						}
					}
					
				}
				
			}else if(type_ == 2){
				
				groupIds = new ArrayList<Long>();
				List<CommunityExtra> commCollege = getByType(6, category);
				for(CommunityExtra extra : commCollege){
					groupIds.add(extra.getGroupId());
					
				}
				for(int i=0; i<groupIds.size(); i++){
					for(Group group : groupList){
						if(group.getGroupId() == groupIds.get(i)){
							results.add(group);
						}					
					}
				}
								
			}else if(type_ == 3){
				
				groupIds = new ArrayList<Long>();
				List<CommunityExtra> commMainstay = getByType(7, category);
				for(CommunityExtra extra : commMainstay){
					groupIds.add(extra.getGroupId());
					
				}
				for(int i=0; i<groupIds.size(); i++){
					for(Group group : groupList){
						if(group.getGroupId() == groupIds.get(i)){
							results.add(group);
						}					
					}
				}
			}			
			
			return results;
		}else{
			return groupList;
		}
		
	}	
	
	
	public List<CommunityExtra> getByType(int type, int category) throws SystemException{
		
		return communityExtraPersistence.findByType(type, category);
	}
}
